#include<stdio.h>
#include<malloc.h>

int *a;
int size=0;
int ind;
void append(int n)
{
	a=realloc(a,sizeof(int)*(size+1));
	a[size]=n;
	size++;
}
void insert(int n,int p)
{
	int i,j;
        char ch;
        if(p>size)
        {printf("largest index value exeeded,largest value was%d\n",size-1);}
        else if(size==0 && p!=0)
        {
		printf("there is no element in the list,if u wnt to insert the element in the 1st location enter 'y'\n");
                scanf("%c",&ch);
                if(ch=='y'||ch=='Y')
                {
                    a=realloc(a,sizeof(int)*(size+1));
		    a[size]=n;
                    size++;
		}




	  }
        else if(p==size)
        {
                    a=realloc(a,sizeof(int)*(size+1));
                    a[size]=n;
                    size++;
         }   
        else
        {
            a=realloc(a,sizeof(int)*(size+1));
            for(j=size;j>p;j--)
            {
                a[j]=a[j-1];
            }
	
            a[p]=n;
            size++;
        }
}
void extend(int arr[],int sz)
{
	int i,j;
	a=realloc(a,sizeof(int)*(size+sz));
	size=size+sz;
	for(i=size-sz,j=0;i<size,j<sz;i++,j++)
	{
		a[i]=arr[j];
	}
}
void remov(int in)
{
	int i,f,j,flag=0;
	for (j=0;j<=size;j++)
	{
		if(in==a[j])
		{
			flag=1;
			f=j;
			break;
		}
	}
	if (flag==0)
	printf("element does not exist\n");
	else
	{
	
		for (i=f+1;i<size;i++)
		{
			a[i-1]=a[i];
		}
		a=realloc(a,sizeof(int)*(size-1));
		size--;
	}
}
void pop()
{
        int ind,i;
        printf("enter the index of the no. to be deleted\n");
        printf("if u want to delete the last element enter '-1' \n");
        scanf("%d",&ind);
	if (ind ==-1)
	{
        printf("the no. u want to delete is %d\n",a[size-1]);
	size--;
	a=realloc(a,sizeof(int)*(size-1));
	}
        else if(ind>=size)
        {
                printf("index is too high, highest index is %d\n",size-1);
        }
	else
	{
		printf("the no. u want to delete is %d\n",a[ind]);
		for (i=ind+1;i<size;i++)
		{
			a[i-1]=a[i];
		}
		a=realloc(a,sizeof(int)*(size-1));
		size--;
	}
}
void reverse()
{
	int i,temp;
        if(size==0)
        {
            printf("the list is empty\n");
        }
        else
        {
	for (i=0;i<size/2;i++)
	{
		temp=a[size-i-1];
		a[size-i-1]=a[i];
		a[i]=temp;
		
	}
        }
        
        
}
void count(x)
{	int m=0,i;
	for(i=0;i<size;i++)
	{
		if(x==a[i])
			m++;
	}
	printf("\n\n\n%d",m);
        
}
        
void indexing(x)
{
	int i,l,flag=0;
	for(i=0;i<size;i++)
	{
		if(a[i]==x)
		{
		l=i;
		flag=1;
		break;
		}
	}
	if(flag==0)
	printf("element does not exist\n");
	else
	printf("the index is \n%d\n",l);
}
void display()
{
	int x,y=0;
        if(size==0)
        {
            printf("no element exists\n");
        }
        else
        {
         printf("[");   
	for(x=0;x<size;x++)
	 {
		printf("%d,",a[x]);
		if(y!=size-1)
		    {
		        printf(",");
		    }
		    else
		    {
		        printf("");
		    }
		    y++;
	  }
         printf("]");
        }
}	
void sort(n)
{
	char ch;
	int i,j,temp,k,l;
	printf("\npress 'i' for sorting in increasing order and 'd' for printing in decreasing order\n");
	scanf("%c",&ch);
	if (ch=='i'||ch=='I')
	{
	    for(i=0;i<n-1;i++)
	    {
		for(j=i+1;j<n;j++)
		{
		    if(a[i] > a[j])
		    {
			temp=a[i];
			a[i]=a[j];
			a[j]=temp;
		    }
		}
	    }
	}
	else if(ch=='d'||ch=='D')
	{
	    for(k=0;k<n-1;k++)
            {
                for(l=k+1;l<n;l++)
                {
                    if(a[k]<a[l])
                    {
                        temp=a[k];
                        a[k]=a[l];
                        a[l]=temp;
                    }
                }
            }
        }
        else
            printf("the character entered is not correct\n");
}

void main()
{

	 char ch='y';
	 int n,no1,no2,in1,no3,no4,no5,sez,i;
	 int b[20];
	 do
	 {
	     printf("enter 1 to append an element\n");
	     printf("enter 2 to insert an element\n");
	     printf("enter 3 to remove an element\n");
	     printf("enter 4 to pop an element\n");
	     printf("enter 5 to find the index of an element\n");
	     printf("enter 6 to count the occurence of an element\n");
	     printf("enter 7 to display the list of element\n");
	     printf("enter 8 to reverse  the list\n");
	     printf("enter 9 to extend the list of  elements\n");
	     printf("enter 10 to sort the list of elements\n");
	     
	     printf("please enter ur choice\n");
	     scanf("%d",&n);
	     switch(n)
	     {
		 case 1:
		     printf("enter the element u want to append in the list\n");
		     scanf("%d",&no1);
		     append(no1);
		     break;
		 case 2:
		     printf("enter the element u want to insert in the list\n");
		     scanf("%d",&no2);
		     printf("enter the index where u want to insert the element in the list\n");
		     printf("remember index starts from 0");

		     scanf("%d",&in1);
		     insert(no2,in1);
		     break;
		 case 3:
		     printf("enter the element u want to remove from the list\n");
		     scanf("%d",&no3);
		     remov(no3);
		     break;
		 case 4:
		     pop();
		     break;
		 case 5:
		     printf("enter the element whose index  u want to know\n");
		     printf("rememember only the index of first ocurence of the no. is given\n");
		     scanf("%d",&no4);
		     indexing(no4);
		     break;
		 case 6:
		     printf("enter the element in the list whose no. of occurences in the list iu want to know\n");
		     scanf("%d",&no5);
                     count(no5);
		     break;
		 case 7:
		     printf("here are the elements in the list \n");
		     display();
		     break;
		 case 8:
		     printf("here is the reverse of the list\n");
		     printf("now the list has become the reversed list,further opereation will be performed on it only\n");
		     reverse();
		     break;
		case 9:
		     printf("enter the size of the array u want to enter\n");
		     scanf("%d",&sez);
		     for(i=0;i<sez;i++)
		     {
			 printf("enter the %d element",i+1);
			 scanf("%d",&b[i]);

		     }
		     extend(b,sez);
		     break;
		case 10:
		     sort(size);
		     break;
		
                default : printf("please only enter the no. of the choices provided\n");
                          break;
            }
            printf("do u wnt to continue (y/n)");
            scanf("%s",&ch);
	    
            if(ch=='y'||ch=='Y')
	    {
		system("clear");
	    
		}
            
        }while(ch=='y'||ch=='Y');
                 
                     
                 
             


}	
		
		
